﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Qiang.Geometry
{
    /// <summary>
    /// 表示二维、三维几何图形的范围
    /// </summary>
    public class Envelope : IEnvelope
    {


        #region 初始化

        public Envelope()
        {
            Init();
        }


        public Envelope(Coordinate pt)
        {
            if (pt == null)
            {
                Init();
                return;
            }
            _minimum = pt;
            _maximum = pt;
        }

        public Envelope(Coordinate pt1, Coordinate pt2)
        {
            if (pt1 == null || pt2 == null)
            {
                Init();
                return;
            }
            _minimum = new Coordinate(Math.Min(pt1.X, pt2.X), Math.Min(pt1.Y, pt2.Y));
            _maximum = new Coordinate(Math.Max(pt1.X, pt2.X), Math.Max(pt1.Y, pt2.Y));
        }


        public Envelope(double minX, double minY, double maxX, double maxY)
        {
            _minimum = new Coordinate(minX, minY);
            _maximum = new Coordinate(maxX, maxY);
        }


        void Init()
        {
            _minimum = new Coordinate(0, 0);
            _maximum = new Coordinate(1, 1);
        }


        #endregion


        #region 属性

        Coordinate _maximum;

        /// <summary>
        /// 
        /// </summary>
        public Coordinate Maximum
        {
            get { return _maximum; }
        }

        Coordinate _minimum;

        /// <summary>
        /// 
        /// </summary>
        public Coordinate Minimum
        {
            get { return _minimum; }         
        }
                
        
        /// <summary>
        /// 维度
        /// </summary>
        public int Dimension
        {
            get
            {
                return Math.Min(_minimum.Dimension, _maximum.Dimension);
            }
        }


        #endregion


        #region 方法

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IPolygon ToPolygon()
        {
            List<Coordinate> coors = new List<Coordinate>();
            coors.Add(_minimum);
            coors.Add(new Coordinate(_minimum.X, _maximum.Y));
            coors.Add(_maximum);
            coors.Add(new Coordinate(_maximum.X, _minimum.Y));
            IPolygon polygon = new Polygon(coors);
            return polygon;
        }


        #endregion


    }
}
